﻿using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Script.Serialization;
using Weapp.Client.Models;

namespace Weapp.Client
{
    public class WeappWebServiceClient : IWeappWebServiceClient
    {
        public IEnumerable<SizeCategoryModel> GetAllSizeCategories()
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:5650/api/SizeCategory/");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var serializer = new JavaScriptSerializer();
            var results = serializer.Deserialize<IEnumerable<SizeCategoryModel>>(content);
            return results;
            
        }

        public Models.SizeCategoryModel GetSizeCategory(int id)
        {
            
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:5650/api/SizeCategory/" + id.ToString());
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var serializer = new JavaScriptSerializer();
            var results = serializer.Deserialize<SizeCategoryModel>(content);
            return results;
        }

        public SizeCategoryModel NewSizeCategory(SizeCategoryModel sizeCategoryModel)
        {
            var serializer = new JavaScriptSerializer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:5650/api/SizeCategory/");
            request.Method = HttpMethod.Post.Method;
            request.Accept = "application/json";
            request.ContentType = "application/json";
            string json = serializer.Serialize(sizeCategoryModel);
            request.ContentLength = json.Length;
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.Created)
            {
                string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
                var results = serializer.Deserialize<SizeCategoryModel>(content);
                return results;
            }
            return null;
        }

        public SizeCategoryModel UpdateSizeCategory(SizeCategoryModel sizeCategoryModel)
        {
            var serializer = new JavaScriptSerializer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:5650/api/SizeCategory/" + sizeCategoryModel.SizeCategoryId);
            request.Method = HttpMethod.Put.Method;
            request.Accept = "application/json";
            request.ContentType = "application/json";
            string json = serializer.Serialize(sizeCategoryModel);
            request.ContentLength = json.Length;
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.Created)
            {
                string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
                var results = serializer.Deserialize<SizeCategoryModel>(content);
                return results;
            }
            return null;
        }


        public bool NewUserDnd(string acc,string pass,string name,string role)
        {
            UserDnDModel userDnDModel = new UserDnDModel();
            userDnDModel.Account = acc;
            userDnDModel.Password = pass;
            userDnDModel.UserName = name;
            userDnDModel.Role = role;
            var serializer = new JavaScriptSerializer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:5650/api/UserDnD/");
            request.Method = HttpMethod.Post.Method;
            request.Accept = "application/json";
            request.ContentType = "application/json";
            string json = serializer.Serialize(userDnDModel);
            request.ContentLength = json.Length;
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
            }
             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.Created)
            {
                return true;
            }
            return false;
            throw new System.NotImplementedException();
        }
    }
}
